///////////////////////////////////////////////////
// Election Administration Polarization
// Make RD Table for Balance on Covariates
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

* Bring in the presidential election analysis data
use "$path/analysis_data/rd_election_analysis_data.dta", clear
keep if office=="pres"

* Loop over the four regression types and estimate the degree of impbalance across all covariates
local i = 0
foreach v in log_pop share_nhwhite share_black region_s region_w {

	*
	local i = `i' + 1

	rdbwselect `v' rv, vce(cluster election_id) kernel(uniform)
	gl bw = e(h_mserd)
	
	* Spec 1, Simple Outcome
	reg `v' treat rv treat_rv if abs(rv)<$bw, vce(clust election_id)
	rdtab `v' treat rv election_id, est_command("reg") new("B`i'")

	* Spec 2, Simple Outcome
	reg `v' treat rv treat_rv if abs(rv)<$bw*2, vce(clust election_id)
	rdtab `v' treat rv election_id, est_command("reg") old("B`i'")

	* Spec 3, Simple Outcome
	reg `v' treat rv treat_rv if abs(rv)<$bw*0.5, vce(clust election_id)
	rdtab `v' treat rv election_id, est_command("reg") old("B`i'")

	* Spec 4, Simple Outcome
	rdrobust `v' rv, vce(cluster election_id)
	rdtab `v' treat rv election_id, est_command("rdrobust") old("B`i'")
}



qui {
* Build the memo/paper table
cap log close
set linesize 255
log using "$path/output/rd_table_covs.tex", text replace
noi di "\begin{table}[ht]"
noi di "\centering"
noi di "\caption{\textbf{Regression Discontinuity Balances County-Level Covariates.}"
noi di "\label{tab:rd_table_covs}}"
noi di "\begin{tabular}{lcccc}"
noi di "\toprule \toprule"
noi di " Outcome Variable & \multicolumn{4}{c}{Balance at RD Cut Point}\\"
noi di "  & (1) & (2) & (3) & (4) \\"
noi di "\midrule"
noi di "Log(Population) & " %4.3f B1[1,1] " & " %4.3f B1[1,2] " & " %4.3f B1[1,3] " & " %4.3f B1[1,4] "\\"
noi di " & (" %4.3f B1[2,1] ") & (" %4.3f B1[2,2] ") & (" %4.3f B1[2,3] ") & (" %4.3f B1[2,4] ") \\"
noi di " & [" %3.0f B1[3,1] "] & [" %3.0f B1[3,2] "] & [" %3.0f B1[3,2] "] & [" %3.0f B1[3,2] "] \\[2mm]"
noi di "Share Non-Hispanic White & " %4.3f B2[1,1] " & " %4.3f B2[1,2] " & " %4.3f B2[1,3] " & " %4.3f B2[1,4] "\\"
noi di " & (" %4.3f B2[2,1] ") & (" %4.3f B2[2,2] ") & (" %4.3f B2[2,3] ") & (" %4.3f B2[2,4] ") \\"
noi di " & [" %3.0f B2[3,1] "] & [" %3.0f B2[3,2] "] & [" %3.0f B2[3,2] "] & [" %3.0f B2[3,2] "] \\[2mm]"
noi di "Share Black & " %4.3f B3[1,1] " & " %4.3f B3[1,2] " & " %4.3f B3[1,3] " & " %4.3f B3[1,4] "\\"
noi di " & (" %4.3f B3[2,1] ") & (" %4.3f B3[2,2] ") & (" %4.3f B3[2,3] ") & (" %4.3f B3[2,4] ") \\"
noi di " & [" %3.0f B3[3,1] "] & [" %3.0f B3[3,2] "] & [" %3.0f B3[3,2] "] & [" %3.0f B3[3,2] "] \\[2mm]"
noi di "South & " %4.3f B4[1,1] " & " %4.3f B4[1,2] " & " %4.3f B4[1,3] " & " %4.3f B4[1,4] "\\"
noi di " & (" %4.3f B4[2,1] ") & (" %4.3f B4[2,2] ") & (" %4.3f B4[2,3] ") & (" %4.3f B4[2,4] ") \\"
noi di " & [" %3.0f B4[3,1] "] & [" %3.0f B4[3,2] "] & [" %3.0f B4[3,2] "] & [" %3.0f B4[3,2] "] \\[2mm]"
noi di "West & " %4.3f B5[1,1] " & " %4.3f B5[1,2] " & " %4.3f B5[1,3] " & " %4.3f B5[1,4] "\\"
noi di " & (" %4.3f B5[2,1] ") & (" %4.3f B5[2,2] ") & (" %4.3f B5[2,3] ") & (" %4.3f B5[2,4] ") \\"
noi di " & [" %3.0f B5[3,1] "] & [" %3.0f B5[3,2] "] & [" %3.0f B5[3,2] "] & [" %3.0f B5[3,2] "] \\[2mm]"
noi di "\midrule"
noi di "Bandwidth Selection & CCT & CCT*2 & CCT/2 & CCT \\"
noi di "Kernel & Uniform & Uniform & Uniform & Triangular \\"
noi di "\bottomrule \bottomrule"
noi di "\multicolumn{5}{p{.72\textwidth}}{\footnotesize Each unbracketed number is an estimate of balance for a particular variable at "
noi di "the discontinuity using a given RD estimator. Robust standard errors clustered by clerk election in parentheses. "
noi di "Sample size reported in square braces. CCT refers to \cite{calonico2014robust} bandwidth selection procedure.}"
noi di "\end{tabular}"
noi di "\end{table}"
log off
}

